﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;

namespace SearchingComponents.SearchingComponents
{
	public partial class SearchingComponentsForm : System.Windows.Forms.Form
	{
		public SearchingComponentsForm(ExternalCommandData cmdData)
		{
			this.m_Searcher = new ComponentsSearcher(cmdData);
			this.InitializeComponent();
		}

		private void btnSearch_Click(object sender, EventArgs e)
		{
			this.Search();
		}

		private void btnClose_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void SearchingComponentsForm_Load(object sender, EventArgs e)
		{
			this.ReadData();
			this.InitializeRegularList();
		}

		private void SearchingComponentsForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveData();
		}

		private void dbgInfoList_CellClick(object sender, DataGridViewCellEventArgs e)
		{
		}

		private void InitializeRegularList()
		{
			List<string> categoryRegularList = this.m_Searcher.GetCategoryRegularList();
			categoryRegularList.Sort();
			this.cmbCategory.Items.Clear();
			this.cmbCategory.Items.Add(ComponentsSearcher.AllCategoryValue);
			foreach (string item in categoryRegularList)
			{
				this.cmbCategory.Items.Add(item);
			}
			this.cmbCategory.SelectedIndex = 0;
			List<string> materialRegularList = this.m_Searcher.GetMaterialRegularList();
			materialRegularList.Sort();
			this.cmbMaterial.Items.Clear();
			this.cmbMaterial.Items.Add(ComponentsSearcher.AllMaterialValue);
			foreach (string item2 in materialRegularList)
			{
				this.cmbMaterial.Items.Add(item2);
			}
			this.cmbMaterial.SelectedIndex = 0;
			List<string> familySymbolRegularList = this.m_Searcher.GetFamilySymbolRegularList(this.cmbCategory.Text, true);
			familySymbolRegularList.Sort();
			this.cmbFamilySymbol.Items.Clear();
			this.cmbFamilySymbol.Items.Add(ComponentsSearcher.AllFamilySymbolValue);
			foreach (string item3 in familySymbolRegularList)
			{
				this.cmbFamilySymbol.Items.Add(item3);
			}
			this.cmbFamilySymbol.SelectedIndex = 0;
		}

		private void UpdateDataGridView(List<ElementInfo> searchRes)
		{
			this.dbgInfoList.Rows.Clear();
			foreach (ElementInfo elementInfo in searchRes)
			{
				DataGridViewRow dataGridViewRow = new DataGridViewRow();
				DataGridViewTextBoxCell dataGridViewTextBoxCell = new DataGridViewTextBoxCell();
				dataGridViewTextBoxCell.Value = elementInfo.Id;
				dataGridViewRow.Cells.Add(dataGridViewTextBoxCell);
				DataGridViewTextBoxCell dataGridViewTextBoxCell2 = new DataGridViewTextBoxCell();
				dataGridViewTextBoxCell2.Value = elementInfo.TheCategory;
				dataGridViewRow.Cells.Add(dataGridViewTextBoxCell2);
				DataGridViewTextBoxCell dataGridViewTextBoxCell3 = new DataGridViewTextBoxCell();
				dataGridViewTextBoxCell3.Value = elementInfo.TheFamily;
				dataGridViewRow.Cells.Add(dataGridViewTextBoxCell3);
				DataGridViewTextBoxCell dataGridViewTextBoxCell4 = new DataGridViewTextBoxCell();
				dataGridViewTextBoxCell4.Value = elementInfo.TheFamilySymbol;
				dataGridViewRow.Cells.Add(dataGridViewTextBoxCell4);
				this.dbgInfoList.Rows.Add(dataGridViewRow);
			}
		}

		private List<string> GetSearchKeyList()
		{
			string[] array = this.tbSeachString.Text.Split(new char[]
			{
				'/',
				'\\'
			});
			List<string> list = new List<string>();
			foreach (string item in array)
			{
				list.Add(item);
			}
			return list;
		}

		private bool ValidateSearchKey(List<string> searchKeyList)
		{
			return true;
		}

		private void ReadData()
		{
			string path = Path.GetTempPath() + "SearchingComponentsForm.dat";
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				this.chkbZoomToSel.Checked = true;
				return;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			int x = Convert.ToInt32(streamReader.ReadLine());
			int y = Convert.ToInt32(streamReader.ReadLine());
			base.Location = new System.Drawing.Point(x, y);
			this.chkbZoomToSel.Checked = Convert.ToBoolean(streamReader.ReadLine());
			streamReader.Close();
			fileStream.Close();
		}

		private void SaveData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "SearchingComponentsForm.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.WriteLine(this.chkbZoomToSel.Checked);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void cmbCategory_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.cmbCategory.Text.CompareTo("") == 0)
			{
				return;
			}
			List<string> familySymbolRegularList = this.m_Searcher.GetFamilySymbolRegularList(this.cmbCategory.Text, true);
			familySymbolRegularList.Sort();
			this.cmbFamilySymbol.Items.Clear();
			this.cmbFamilySymbol.Items.Add(ComponentsSearcher.AllFamilySymbolValue);
			foreach (string item in familySymbolRegularList)
			{
				this.cmbFamilySymbol.Items.Add(item);
			}
			this.cmbFamilySymbol.SelectedIndex = 0;
		}

		private void Search()
		{
			List<string> searchKeyList = this.GetSearchKeyList();
			if (searchKeyList.Count == 1 && searchKeyList[0].CompareTo("") == 0 && this.cmbCategory.Text.CompareTo(ComponentsSearcher.AllCategoryValue) == 0 && this.cmbFamilySymbol.Text.CompareTo(ComponentsSearcher.AllFamilySymbolValue) == 0 && this.cmbMaterial.Text.CompareTo(ComponentsSearcher.AllMaterialValue) == 0)
			{
				YJKMessageBox.Show("查找内容为空!");
				return;
			}
			if (!this.ValidateSearchKey(searchKeyList))
			{
				YJKMessageBox.Show("查找内容格式错误!");
				return;
			}
			string text = this.cmbCategory.Text;
			string text2 = this.cmbMaterial.Text;
			string text3 = this.cmbFamilySymbol.Text;
			List<ElementInfo> list = this.m_Searcher.Search(searchKeyList, text, text2, text3);
			this.UpdateDataGridView(list);
			this.lbSelectedCount.Text = "找到" + list.Count.ToString() + "个构件";
		}

		private void tbSeachString_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Return)
			{
				this.Search();
			}
		}

		private void dbgInfoList_SelectionChanged(object sender, EventArgs e)
		{
			if (this.dbgInfoList.SelectedRows.Count < 1)
			{
				return;
			}
			ElementId elementId = new ElementId(Convert.ToInt32(this.dbgInfoList.SelectedRows[0].Cells[0].Value.ToString()));
			if (!this.m_Searcher.IsVisibleInActiveView(elementId))
			{
				this.m_Searcher.ActiveElementView(elementId);
			}
			if (this.chkbZoomToSel.Checked)
			{
				this.m_Searcher.ZoomToElement(elementId);
			}
			this.m_Searcher.HighlightShowElements(elementId, base.Handle);
		}

		private void tbSeachString_KeyPress(object sender, KeyPressEventArgs e)
		{
		}

		private void SearchingComponentsForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private ComponentsSearcher m_Searcher;
	}
}
